package com.ming.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : TranFrame
 * @Description : 坦克窗口
 * @Author : H.M
 * @Date: 2021-06-09 19:18
 **/
public class TankFrame extends Frame {

    Dir dir = Dir.DOWN;
    Tank myTank = new Tank(200, 400, Dir.DOWN,Group.GOOD,this);
    Bullet bullet = new Bullet(300,300,Dir.DOWN,Group.GOOD,this);
    static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;
    List<Bullet> bullets = new ArrayList();
   // Explode e = new Explode(100,100,this);
    List<Explode> explodes = new ArrayList<>();

    /**
     * 敌方坦克
     */
   public  List<Tank> tanks = new ArrayList();

    public TankFrame() throws HeadlessException {
        setSize(GAME_WIDTH , GAME_HEIGHT);
        setResizable(false);
        setTitle("TANK WAR");
        setVisible(true);
        this.addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    Image offScreenImage = null;
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color  c =  gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);

    }
    @Override
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.white);
        g.drawString("子弹数量："+bullets.size(),10,60);
        g.drawString("敌方坦克数量："+tanks.size(),10,80);
        g.setColor(c);
        myTank.paint(g);
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        //爆炸
        for (int i = 0; i <explodes.size() ; i++) {
            explodes.get(i).paint(g);
        }
        //碰撞检测
        for (int i = 0; i <bullets.size() ; i++) {
            for (int j = 0; j < tanks.size() ; j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }


    }

    /**
     * 键盘监听类
     */
    class MyKeyListener extends KeyAdapter {
        boolean bL = false;
        boolean bR = false;
        boolean bU = false;
        boolean bD = false;

        /**
         * Invoked when a key has been pressed.
         *
         * @param e
         */
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    setMainTankDir();
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                default:
                    break;
            }
            new Thread(()->new Audio("audio/tank_move.wav").play()).start();
        }

        /**
         * Invoked when a key has been released.
         *
         * @param e
         */
        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }


        private void setMainTankDir() {
            if (!bL && !bU && !bR && !bD) {
                myTank.setMoving(false);
            }else {
                myTank.setMoving(true);
            }
            if (bL) {
               myTank.setDir(Dir.LEFT);
            }
            if (bR) {
                myTank.setDir(Dir.RIGHT);
            }
            if (bU) {
                myTank.setDir(Dir.UP);
            }
            if (bD) {
                myTank.setDir(Dir.DOWN);
            }
        }
    }


}
